package com.carey.myblog.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.carey.myblog.pojo.BlogTags;
import com.carey.myblog.pojo.Tag;
import com.carey.myblog.service.BlogService;
import com.carey.myblog.service.BlogTagsService;
import com.carey.myblog.service.TagService;
import com.carey.myblog.vo.BlogQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

@Controller
public class TagShowController {
    @Autowired
    private TagService tagService;

    @Autowired
    private BlogService blogService;

    @Autowired
    private BlogTagsService blogTagsService;

    @GetMapping("/tags/{id}")
    public String tags(Pageable pageable,@PathVariable Long id, Model model) {
        List<Tag> tags = tagService.listTagTop(10000);
        if (id == -1) {
            id = tags.get(0).getId();
        }
        BlogQuery blogQuery = new BlogQuery();
        blogQuery.setPublished(true);
        QueryWrapper<BlogTags> wrapper = new QueryWrapper<>();
        wrapper.eq("tags_id",id);
        List<BlogTags> blogTags = blogTagsService.list(wrapper);
        model.addAttribute("tags", tags);
        model.addAttribute("page", blogService.listBlog(blogTags,pageable));
        model.addAttribute("activeTagId", id);
        return "tags";
    }
}
